<?php

namespace backend\controllers;

use Yii;
use common\models\Order;
use yii\data\ActiveDataProvider;
use backend\controllers\base\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\Pagination;
use common\models\Bom;

/**
 * OrderController implements the CRUD actions for Order model.
 */
class OrderController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Order models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model=Order::find();
        $pages=new Pagination(['totalCount'=>$model->count(),'pagesize'=>20]);
        $data=$model->offset($pages->offset)->limit($pages->limit)->all();
        return $this->render('index', [
            'data' => $data,
            'pages'=>$pages
        ]);
    }

    /**
     * Displays a single Order model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Order model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Order();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    public function actionBom(){
        $data=Bom::find()->select('flag,id')->asArray()->all();
        echo json_encode($data);exit;
    }

    public function actionSend(){
        $post=Yii::$app->request->post();
        $id=(int)$post['id'];
        $model=Order::findOne($id);
        $flag=false;
        if(isset($post['bid']) && isset($post['waybill'])){
            $bom=Bom::findOne($post['bid']);
            if($bom){
                $model->express=$bom->id;
                $model->status=2;
                $model->waybill=$post['waybill'];
                if($model->save()){
                    $content='【锐锋堂刀剑】尊敬的用户，您好，您的订单'.$model->omun.'已经发货了,快递公司是'.$bom->flag.',快递单号为'.$post['waybill'].',通过锐锋堂刀剑官网的订单详情可以快速查询物流详情';
                    $this->sendCode($model->tel,$content);
                    $flag=true;
                }
            }
        }
        echo json_encode($flag);exit;
    }
    /**
     * Updates an existing Order model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Order model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Order model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Order the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Order::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
